package com.xiaocikuai.smallchunks.service.Transaction;

import com.alibaba.fastjson.JSONObject;
import com.xiaocikuai.smallchunks.entity.Transaction.Address;
import com.xiaocikuai.smallchunks.mapper.Transaction.TransactionMapper;
import com.yl.jms.sdk.JtExpressApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;



@Service
public class JTEService {

    @Resource
    private JtExpressApi jtExpressApi;

    @Resource
    private TransactionMapper transactionMapper;

    public static final String addUrl = "https://openapi.jtexpress.com.cn/webopenplatformapi/api/order/addOrder";

    public JSONObject addOrder(int aid, String orderId, String tName, String version) throws IOException {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> sender = new HashMap<>();
        Map<String, Object> receiver = new HashMap<>();
        Map<String, Object> items = new HashMap<>();
        map.put("txlogisticId",orderId);//客户订单号（传客户自己系统的订单号）
        map.put("network","6539187");//合作网点编码（没有则不传）
        map.put("expressType","EZ");//快件类型：EZ(标准快递),TYD(兔优达)
        map.put("orderType","1");//订单类型（有客户编号为月结）1、 散客；2、月结；
        map.put("serviceType","01");//服务类型 ：02 门店寄件 ； 01 上门取件
        map.put("deliveryType","03");//派送类型： 06 代收点自提 05 快递柜自提 04 站点自提 03 派送上门
        map.put("payType","PP_PM");//支付方式：PP_PM("寄付月结"), CC_CASH("到付现结");
        map.put("goodsType","bm000001");
        map.put("weight","1");

        sender.put("name","张舟");
        sender.put("mobile","19992787088");
        sender.put("phone","");
        sender.put("countryCode","CHN");
        sender.put("prov","山东省");
        sender.put("city","临沂市");
        sender.put("area","河东区");
        sender.put("address","山东省临沂市河东区九曲街道王家斜坊极兔速递");
        map.put("sender",sender);

        items.put("itemType","bm000001");
        items.put("itemName",tName + "【" + version + "】");
        items.put("number","1");
        items.put("desc","【小词块】微语境背单词");
        map.put("items",items);

        Address address = transactionMapper.getAddressInfo(aid);
        receiver.put("name", address.getConsignee());
        receiver.put("mobile", address.getTelephone());
        receiver.put("phone", "");
        receiver.put("countryCode", "CHN");
        receiver.put("prov", address.getProv());
        receiver.put("city", address.getCity());
        receiver.put("area", address.getArea());
        receiver.put("address", address.getProv() + address.getCity() + address.getArea() + address.getDelivery());
        map.put("receiver", receiver);

        return jtExpressApi.postByCustom(map, addUrl);
    }







}
